package com.bill.gaty.mfidelity.activities.fragments;

import java.net.URLEncoder;

import com.bill.gaty.mfidelity.R;
import com.bill.gaty.mfidelity.activities.CompatibiliteActivity;
import com.bill.gaty.mfidelity.beans.Compatibilite;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class CompatibiliteFragment extends Fragment {
	@Override
	public void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		outState.putSerializable("compatibilite", comp);
		super.onSaveInstanceState(outState);
	}
	private EditText elle, lui;
	private Button verifier;
	private WebView resultat;
	private TextView aboutcompatibiliteview;
	private Compatibilite comp;
	public final static int CompatibiliteFragment_activity_result_code = 0x001223;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		 
		View v = inflater.inflate(R.layout.compatibilite, null);

		elle = (EditText) v.findViewById(R.id.compatibilite_elle);
		lui = (EditText) v.findViewById(R.id.compatibilite_lui);
		verifier = (Button) v.findViewById(R.id.compatibilite_valider);
		resultat = (WebView) v
				.findViewById(R.id.compatibilite_resultat_textview);
		// resultat.getSettings().setJavaScriptEnabled(true);

		aboutcompatibiliteview = (TextView) v
				.findViewById(R.id.compatibility_about_message_textview);
		aboutcompatibiliteview
				.setText(R.string.compatibility_about_message_textview_label);
		verifier.setOnClickListener(new View.OnClickListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				try{
				// TODO Auto-generated method stub
				String luiString=URLEncoder.encode((null!=lui.getText()?lui.getText().toString().trim():""));
				String elleString=URLEncoder.encode((null!=elle.getText()?elle.getText().toString().trim():""));
				
				if ( !(URLEncoder.encode(luiString).concat(URLEncoder.encode(elleString)).trim().compareToIgnoreCase(URLEncoder.encode("").trim())==0)) {
					Intent job = new Intent(getActivity(),
							CompatibiliteActivity.class);
					Bundle b = new Bundle();
					Compatibilite com = new Compatibilite();
					com.setElle(elleString);
					com.setLui(luiString);
					b.putSerializable("Compatibilite", com);
					job.putExtras(b);
					startActivityForResult(job,
							CompatibiliteFragment_activity_result_code);
				}
				}catch (Exception e) {
					// TODO: handle exception
				}
			}
		});
		setview();
		return v;

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (resultCode == Activity.RESULT_OK
				&& requestCode == CompatibiliteFragment_activity_result_code) {
			Log.i("CompatibiliteFragment.onActivityResult", "receive response");
			comp = (Compatibilite) data.getExtras().get("Compatibilite");
			setview();

		}
	}
public void setview(){
	if (null != comp) {
		Log.i("Compatibilite", comp.toString());
		if (comp.isOk()) {
			resultat.loadData("<html>" + comp.getResult() + "</html>",
					"text/html", "UTF-8");
		} else {
			resultat.loadData(
					"<html>"
							+ getResources().getString(
									R.string.aucun_result) + "</html>",
					"text/html", "UTF-8");
		}
	} else
		resultat.loadData(
				"<html>"
						+ getResources().getString(
								R.string.aucun_result) + "</html>",
				"text/html", "UTF-8");
}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(null!=savedInstanceState&&savedInstanceState.containsKey("compatibilite"))
			comp=(Compatibilite) savedInstanceState.getSerializable("compatibilite");
		  

	}
}
